var express = require('express');
var router = express.Router();
let mongoose = require('mongoose');
// //student
// //建立student需要的集合模型对象  
// //建立class与模型的关系
// let Student = new mongoose.Schema({
//     name: String,
//     age: String,
//     phone: String,
//     classId: {
//         type: mongoose.SchemaTypes.ObjectId,
//         ref: 'classModel1'
//     }
// })
// let Classname = new mongoose.Schema({
//     name: String
// })
// //建立class与模型的关系
// let classModel1 = mongoose.model('classModel1', Classname, 'class');
// //建立student与模型的关系
// mongoose.model('studentModel', Student, 'student');
// //建立addstudent
// studentaddModel = mongoose.model('studentaddModel', Student, 'student');
// //建立删除模型
// studentdeleteModel = mongoose.model('studentdeleteModel', Student,'student');
// //建立修改模型
// studentchangeModel = mongoose.model('studentchangeModel',Student,'student')
//渲染操作
router.post('/studentshow', async function (req, res, next) {
    let resstudent = await mongoose.model('studentModel').find({}).populate('classId');
    res.send(resstudent);
    // console.log(resstudent);
})

//添加student
router.post('/studentadd', async function (req, res) {
await mongoose.model('studentaddModel').create({
        name: req.body.name,
        age: req.body.age,
        phone: req.body.phone,
        classId: req.body.classId,
    })
    res.send({
        mes: '添加成功'
    })
})
//删除student
router.post('/studentdelate', async function (req, res) {
await mongoose.model('studentdeleteModel').deleteMany({
    _id:mongoose.Types.ObjectId(req.body._id)  
})

        res.send({
            mes: '删除成功'
        })
    })
//修改
router.post('/studentchange',async function (req, res){
    await mongoose.model('studentchangeModel').updateMany({
        _id:req.body._id  
    },{ age: req.body.age,
      name: req.body.name,
      classId: req.body.classId,
      phone: req.body.phone,
     
    });
  
    res.send({
      code:200,
      mes: "修改成功"
    })   
})
module.exports = router;